package com.socialbank.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wqdata.auth.bean.Menu;
import com.wqdata.auth.dto.MenuDto;
import com.wqdata.auth.dto.TreeDto;
import com.wqdata.auth.service.MenuService;
import com.wqdata.auth.util.TreeUtils;
import com.wqdata.auth.util.UserUtils;

@Controller
@RequestMapping("sys/menu")
public class MenuController {

	private static Logger logger = Logger.getLogger(MenuController.class);
	
	@Autowired
	private MenuService menuService;

	@RequestMapping("toMenuList")
	public String toMenuList(HttpServletRequest request,Model model){
		List<Menu> sortMenuList = new ArrayList<>();		
		List<Menu> menuList = menuService.getMenuByUserId(UserUtils.getCurrrentUserId());
		TreeUtils.sortTreeList(sortMenuList, menuList, 0L);
		model.addAttribute("menuList", sortMenuList);
		return "systemConfig/MenuList";
	}
	
	@RequestMapping("toMenuEdit")
	@RequiresPermissions(value = { "menu:add", "menu:update" }, logical = Logical.OR)
	public String toUserEdit(@ModelAttribute("editFlag") int editFlag,Long menuId,Long parentId,Model model){
		//1 为增加 2为修改
		if(editFlag == 2){
			MenuDto menuDto = menuService.getMenuDtoById(menuId);
			model.addAttribute("menu", menuDto);
		}
		if(editFlag == 1){
			if(parentId != null){
				MenuDto menuDto = menuService.getTreeDtoById(parentId);
				model.addAttribute("menu",menuDto);	
			}
		}
		return "systemConfig/MenuAdd";
	}
	
	@RequestMapping("/updateMenu")
	@RequiresPermissions("menu:update")	
	public @ResponseBody Map<String, Object> updateMenu(@RequestBody Menu menu){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
			menuService.updateMenu(menu);
			resultMap.put("code", 200);
			resultMap.put("message", "修改菜单信息成功");
		} catch (Exception e) {
			logger.error("修改菜单失败", e);
			e.printStackTrace();
		}		
		return resultMap;
	} 
	@RequestMapping("/saveMenu")
	@RequiresPermissions("menu:add")	
	public @ResponseBody Map<String, Object> saveMenu(@RequestBody Menu menu){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
			menuService.addMenu(menu);
			resultMap.put("code", 200);
			resultMap.put("message", "增加菜单信息成功");
		} catch (Exception e) {
			logger.error("增加菜单失败", e);
			e.printStackTrace();
		}		
		return resultMap;
	} 
	
	//获取所有树形结构 菜单节点信息
	@RequestMapping("/getParentMenuTreeData")
	public @ResponseBody  List<TreeDto> getParentMenuTreeData(Long menuId){
		List<TreeDto> treeList = this.menuService.getAllMenuDto();
		if(menuId!=null){//代表进入的是修改页面
			Map<Long,TreeDto> childrenMap = new HashMap<Long,TreeDto>();
			//1:把他的儿子,孙子全部找出来
			TreeUtils.getAllChildrenMap(childrenMap, treeList, menuId);
			//2:把他的儿子,孙子从列表里面remove
			Iterator<TreeDto> treeIterator = treeList.iterator();
			while(treeIterator.hasNext()){
				TreeDto tree = treeIterator.next();
				//如果子节点列表里面存在这个对象,则删除
				if(childrenMap.get(tree.getId())!=null){
					treeIterator.remove();
				}
				//删除本身
				if(tree.getId().equals(menuId)){
					treeIterator.remove();
				}
			}			
		}
		return treeList;
	}
	
	@RequestMapping("/delMenu")
	@RequiresPermissions("menu:delete")	
	public @ResponseBody Map<String, Object> delDept(Long menuId){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try {
			menuService.delMenu(menuId);
			resultMap.put("code", 200);
			resultMap.put("message", "删除菜单信息成功");
		} catch (Exception e) {
			logger.error("删除菜单失败", e);
			e.printStackTrace();
		}
		return resultMap;
	}

}
